<?php
$article_id = intval($article_id);
switch($action){
    case 'title':

        $cat_id = intval($cat_id);
        $title = trim($title);
        $short_title = trim($short_title);

        $status = 0;
        $info = '操作失败';
        $data = array();

        if($cat_id < 2 || empty($category_list[$cat_id])){
            $info = '请正确选择分类';
        }elseif(empty($title)){
            $info = '请填写标题';
        }elseif(empty($short_title)){
            $info = '请填写短标题';
        }else{
            $cat_ids = get_cat_ids($cat_id);
            $author = trim($author);
            $from = trim($from);
            $from_url = trim($from_url);
            $summary = trim($summary);

            if($article_id > 0 && $db->result_first("SELECT count(id) FROM article_list WHERE id='{$article_id}'") == 1 ){
                $sql = "UPDATE `article_list`
                    SET `cat_id` = '{$cat_id}',
                        `cat_ids` = '{$cat_ids}',
                        `title` = '{$title}',
                        `short_title` = '{$short_title}',
                        `author` = '{$author}',
                        `from` = '{$from}',
                        `from_url` = '{$from_url}',
                        `summary` = '{$summary}',
                        `update_time` = '".time()."',
                        `status` = 'NEED_GENERATE'
                WHERE `id`='{$article_id}'";
                $update = true;
                $info = '修改';
            }else{
                $sql = "INSERT INTO `article_list` (
                                `cat_id` ,`cat_ids` ,`user_id` ,`user_name` ,
                                `title` ,`short_title` ,`author` ,`from` ,`from_url` ,
                                `summary` ,`create_time` ,`status`
                            )
                            VALUES (
                                '{$cat_id}', '{$cat_ids}', '{$_userid}', '{$_username}',
                                '{$title}', '{$short_title}', '{$author}', '{$from}', '{$from_url}',
                                '{$summary}', '".time()."', 'NEED_GENERATE'
                            );";
                $update = false;
                $info = '添加';
            }
            if($db->query($sql)){
                $status = 1;
                $info = $info.'成功';
                $data['id'] = $update ? $article_id : $db->insert_id();
            }else{
                $info = $info.'失败';
            }
        }
        echo ajax_return($status, $info, $data);
        exit();
        break;
    case 'content':
        $content = trim($content);
        $status = $db->result_first("SELECT count(*) FROM `article_list` WHERE `id`='{$article_id}' AND `status`!='DEL'") == 1
                &&  $db->query("UPDATE `article_content` SET `status`='BAK' WHERE `art_id`='{$article_id}' AND `status`='LATEST'")
                &&  $db->query("INSERT INTO `article_content`(`art_id`,`content`,`create_time`,`status`) VALUES('{$article_id}','{$content}','".time()."','LATEST')")
                &&  $db->query("UPDATE `article_list` SET `status`='NEED_GENERATE',`update_time` = '".time()."' WHERE `id`='{$article_id}'")
                ? 1 : 0;
        echo ajax_return($status);
        exit();
        break;
    default:
        if($article_id > 0 ){
            $data = $db->fetch_first("SELECT `at`.*,`ac`.`content` FROM `article_list` `at`
                                    LEFT JOIN `article_content` `ac` ON `ac`.`art_id`=`at`.`id` AND `ac`.`status`='LATEST'
                                    WHERE `at`.`id`='".intval($article_id)."'"
                    );
            $data['cat_name'] = $category_list[$data['cat_id']]['name'];
        }else{
            $data['author'] = $_username;
            $data['from'] = $system_settings['sitename'];
        }
        //CKeditor
        // Include the CKEditor class.
        include_once(APP_ROOT."/include/ckeditor/ckeditor.php");
        // Create a class instance.
        $CKEditor = new CKEditor();//exit("123");
        // Do not print the code directly to the browser, return it instead.
        $CKEditor->returnOutput = true;
        // Path to the CKEditor directory, ideally use an absolute path instead of a relative dir.
        //   $CKEditor->basePath = '/ckeditor/'
        // If not set, CKEditor will try to detect the correct path.
        $CKEditor->basePath = APP_URL.'/include/ckeditor/';
        // Set global configuration (will be used by all instances of CKEditor).
        $CKEditor->config['width'] = '80%';
        // Change default textarea attributes.
        $CKEditor->textareaAttributes = array("cols" => 80, "rows" => 10);
        break;
}
